﻿using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.Splitneck
{
    /// <summary>
    /// 获取分割领用表数据
    /// </summary>
    public class ShowSplitneckHandler : IRequestHandler<ShowSplitneckCommand, Result>
    {
        public ShowSplitneckHandler(IBaseRepositoryGet<SplitRequis> splitRequisRepositoryGet, IBaseRepositoryGet<UPCData> uPCDataRepositoryGet)
        {
            SplitRequisRepositoryGet = splitRequisRepositoryGet;
            UPCDataRepositoryGet = uPCDataRepositoryGet;
        }

        public IBaseRepositoryGet<SplitRequis> SplitRequisRepositoryGet { get; set; }
        public IBaseRepositoryGet<UPCData> UPCDataRepositoryGet { get; set; }
        public async Task<Result> Handle(ShowSplitneckCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            var list = (from a in SplitRequisRepositoryGet.GetAll()
                        join b in UPCDataRepositoryGet.GetAll()
                        on a.MarkId equals b.GoodsMinuteId
                        where (b.CommodityId==11 || b.CommodityId == 12|| b.CommodityId == 13|| b.CommodityId == 14|| b.CommodityId == 15)
                        select new ShowSplitReportDTO
                        {
                            SplitRequisId = a.SplitRequisId,
                            SplitRequisTime = a.SplitRequisTime,
                            DepotId = a.DepotId,
                            ProductId = a.ProductId,
                            MarkId = a.MarkId,
                            SplitRequisNum = a.SplitRequisNum,
                            SplitRequisGrossWeight = a.SplitRequisGrossWeight,
                            SplitRequisTares = a.SplitRequisTares,
                            SplitRequisWeight = a.SplitRequisWeight,
                            SplitRequisRemark = a.SplitRequisRemark,
                            IsDelete = a.IsDelete,
                            GoodsMinuteId = b.GoodsMinuteId,
                            CommodityId=b.CommodityId,
                            UPCDataName=b.UPCDataName
                        }).ToList();
            if (list != null)
            {
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
